package cn.bootx.satoken.plugin.ignore.router;

import cn.bootx.satoken.plugin.ignore.annotation.IgnoreAuth;
import cn.dev33.satoken.fun.SaFunction;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import org.springframework.web.method.HandlerMethod;

import java.util.Objects;

/**
 * 鉴权路由配置类
 * @author xxm
 * @date 2021/8/2
 */
public class IgnoreSaRouteFunction implements SaFunction {
    private final Object handler;

    public IgnoreSaRouteFunction(Object handler) {
        this.handler = handler;
    }

    @Override
    public void run() {
        // 注解跳过鉴权
        if (handler instanceof HandlerMethod){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            IgnoreAuth ignoreAuth = handlerMethod.getMethodAnnotation(IgnoreAuth.class);
            if (Objects.nonNull(ignoreAuth) && ignoreAuth.ignore()){
                SaRouter.stop();
            }
        }
        StpUtil.checkLogin();
    }
}
